import { calcCFigure, calcGFigure, getTrigramFromCFigure, getTrigramFromGFigure, getWuxingFromCFigure, getWuxingFromGFigure } from "plum-cg";


const num_td_c_all = document.querySelectorAll('.num_td_c')
const num_td_g_all = document.querySelectorAll('.num_td_g')
const trigram_td_c_all = document.querySelectorAll('.trigram_td_c')
const trigram_td_g_all = document.querySelectorAll('.trigram_td_g')
const wuxing_td_c_all = document.querySelectorAll('.wuxing_td_c')
const wuxing_td_g_all = document.querySelectorAll('.wuxing_td_g')
const ipt = document.querySelector('.ipt')
ipt.addEventListener('input', (e) => {
    const v = e.target.value.toString();
    if(v.length > 3){
        ipt.value = null
    }
    if(v.length === 3){
        const res = calc(v)
        update(res)
    }
})

function calc(v){
    if(typeof v !== 'string') {
        throw 'bug not string'
    }
    const cf = calcCFigure(v)
    const gf = calcGFigure(v)
    const cf_trigram = getTrigramFromCFigure(cf)
    const gf_trigram = getTrigramFromGFigure(gf)
    const cf_trigram_wuxing = getWuxingFromCFigure(cf)
    const gf_trigram_wuxing = getWuxingFromGFigure(gf)
    return {
        cf,
        gf,
        cf_trigram,
        gf_trigram,
        cf_trigram_wuxing,
        gf_trigram_wuxing,
    }
}

function update(obj){
    console.log(obj);
    num_td_c_all.forEach((item, index) => {
        item.textContent = Array.from(obj.cf.toString().padStart(5, ' '))[index]
    })
    num_td_g_all.forEach((item, index) => {
        item.textContent = Array.from(obj.gf.toString().padStart(5, ' '))[index]
    })
    trigram_td_c_all.forEach((item, index) => {
        if(obj.cf_trigram.length < 5){
            obj.cf_trigram = ['', ...obj.cf_trigram]
        }
        item.textContent = obj.cf_trigram[index]
    })
    trigram_td_g_all.forEach((item, index) => {
        if(obj.gf_trigram.length < 5){
            obj.gf_trigram = ['', ...obj.gf_trigram]
        }
        item.textContent = obj.gf_trigram[index]
    })
    wuxing_td_c_all.forEach((item, index) => {
        if(obj.cf_trigram_wuxing.length < 5){
            obj.cf_trigram_wuxing = ['', ...obj.cf_trigram_wuxing]
        }
        item.textContent = obj.cf_trigram_wuxing[index]
    })
    wuxing_td_g_all.forEach((item, index) => {
        if(obj.gf_trigram_wuxing.length < 5){
            obj.gf_trigram_wuxing = ['', ...obj.gf_trigram_wuxing]
        }
        item.textContent = obj.gf_trigram_wuxing[index]
    })
    
    
}

